<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/19 0019
 * Time: 下午 5:20
 */

namespace App\Http\Classes;

use Illuminate\Support\Facades\DB;

class UserRemember
{
    private $tableName = 'user_remember';
    private $tableDB = null;
    private $error = 0;
    private $message = [
        0 => '无数据',
        1 => '写入数据库出错',
        2 => '更新数据库失败',
        3 => '将要删除的数据不存在'
    ];

    public function __construct( $userId = null )
    {
        $this->tableDB = DB::table( $this->tableName );

        if ( $userId !== null )
        {
            $this->tableDB = $this->tableDB->where('user_id', '=', $userId);
        }
    }

    public function addRememberWords( array $data )
    {
        $result = $this->tableDB->insert($data);

        if ( $result )
        {
            return true;
        }
        else
        {
            $this->error = 1;
            return false;
        }
    }

    public function addRememberSentences( array $data )
    {
        $result = $this->tableDB->insert($data);

        if ( $result )
        {
            return true;
        }
        else
        {
            $this->error = 1;
            return false;
        }
    }

    public function getUserRememberWordsWithPaging(array $data)
    {
        $result = $this->tableDB;
        $count = $this->tableDB->count();
        if ( $count > 0 )
        {
            $orderBy = isset($data['orderBy']) ? $data['orderBy'] : 'id';
            $order = isset($data['order']) ? $data['order'] : 'asc';
            $index = isset($data['index']) ? $data['index'] : 1;
            $limit = isset($data['limit']) ? $data['limit'] : 20;

            if ( $orderBy == 'id')
            {
                $result = $result -> orderBy('user_remember.id', $order);
            }
            else if ( $order == 'time' )
            {
                $result = $result -> orderBy('user_remember.time', $order);
            }
            else
            {
                $result = $result -> orderBy('user_remember.id', $order);
            }

            $result = $result -> join('words', 'words.id', '=', 'user_remember.word_id')
                              -> join('lessons', 'lessons.id', '=', 'words.lesson_id')
                              -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                    'user_remember.time',
                    'user_remember.id',
                    'words.word as content',
                    'lessons.lesson_zh as lesson',
                    'books.book as book'
                );

            $result = $result -> skip( ($index - 1) * $limit ) -> limit($limit) -> get();

            if( $result ){
                $return = [];
                $return['total'] = $count;
                $return['records'] = $result;
                return $return;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }

    }

    public function getUserAllRemmberWords()
    {
        $result = $this->tableDB;

        $result = $result -> join('words', 'words.id', '=', 'user_remember.word_id')
            -> join('lessons', 'lessons.id', '=', 'words.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_remember.time',
                'user_remember.id',
                'words.word as content',
                'lessons.lesson_zh as lesson',
                'books.book as book'
            );

        $result = $result -> get();

        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getUserAllRemmberSentences()
    {
        $result = $this->tableDB;

        $result = $result -> join('sentences', 'sentences.id', '=', 'user_remember.sentence_id')
            -> join('lessons', 'lessons.id', '=', 'sentences.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_remember.time',
                'user_remember.id',
                'sentences.sentence as content',
                'lessons.lesson_zh as lesson',
                'books.book as book'
            );

        $result = $result -> get();

        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getUserAllRememberWordsArray()
    {
        $result = $this->tableDB;

        $result = $result->where('type', 1)->select('word_id')->get();

        if ( $result )
        {
            $words = [];
            for ($i = 0; $i < count($result); $i++)
            {
                $words[] = $result[$i]->word_id;
            }

            return $words;
        }
        else
        {
            return [];
        }
    }

    public function getUserAllRememberSentencesArray()
    {
        $result = $this->tableDB;

        $result = $result->where('type', 2)->select('sentence_id')->get();

        if ( $result )
        {
            $sentences = [];
            for ($i = 0; $i < count($result); $i++)
            {
                $sentences[] = $result[$i]->sentence_id;
            }

            return $sentences;
        }
        else
        {
            return [];
        }
    }

    public function getMessage()
    {
        return $this->message[ $this->error ];
    }
}